package com.bytz.modules.cms.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.common.system.query.QueryGenerator;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.order.entity.OrderArrival;
import com.bytz.modules.cms.order.model.OrderArrivalModel;
import com.bytz.modules.cms.order.service.IAdminOrderArrivalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;

@RestController
@Api(tags = "订单到货情况")
@RequestMapping("/orderArrival")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.OrderArrival.EDIT + "')")
public class OrderArrivalController {
    @Autowired
    private IAdminOrderArrivalService orderArrivalService;

    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<OrderArrival>> page(OrderArrival orderArrival,
                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                     HttpServletRequest req) {

        IPage<OrderArrival> page = orderArrivalService.page(
                new Page<>(pageNo, pageSize),
                QueryGenerator.initQueryWrapper(orderArrival, req.getParameterMap(), OrderArrival.class)
        );
        return Result.ok(page);
    }

    @ApiOperation(value = "新增到货情况(只能更改产品排产中的状态)", notes = "新增到货情况(只能更改产品排产中的状态)")
    @RequestMapping(value = "/updateAndSave", method = RequestMethod.POST)
    public Result<OrderArrivalModel> addArrivalStatus(@RequestBody OrderArrival orderArrival) {
        orderArrivalService.addArrivalStatus(orderArrival);
        return Result.ok();
    }

    @ApiOperation(value = "修改到货情况(只能修改待提交尾款的状态)", notes = "修改(只能修改待提交尾款的状态)")
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public Result<OrderArrivalModel> updateArrivalStatus(@RequestBody OrderArrival orderArrival) {
        orderArrivalService.updateArrivalStatus(orderArrival);
        return Result.ok();
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {
        ExcelUtil.exportTemp("订单到货情况导入模板", "Sheet1", OrderArrival.class, response);
    }

    @ApiOperation(value = "导入到货情况", notes = "导入到货情况")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<Collection<String>> importExcel(HttpServletRequest request) {
        List<OrderArrival> models = ExcelUtil.importGroup(request, OrderArrival.class, null);
        orderArrivalService.importExcel(models);
        return Result.ok();
    }

}
